package com.engine.budget.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class GetYssxdbCmd extends AbstractCommonCommand<Map<String,Object>> {

    public GetYssxdbCmd(User user, Map<String,Object> params) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {

        HashMap<String, Object> req = new HashMap<>();
        //4.定义SQL语句
        String sql="select id,cjnf,sxbm,sxmc,ywlx,ywxl,zz,zdbz,hdnf,glfs,lybzbmc from uf_yssxdb where 1 = 1 ";
        String sqlTotal="select count(1) total from uf_yssxdb where 1 = 1 ";


        ArrayList<String> sqlArrs = new ArrayList<>();
        ArrayList<String> sqlTotalArrs = new ArrayList<>();
        String event = (String)params.get("event");
        if(event != null&&event != ""){
            sql+=(" AND sxbm = ? ");
            sqlTotal+=(" AND sxbm = ? ");
            sqlArrs.add(event);
            sqlTotalArrs.add(event);
        }


        String eventName = (String)params.get("eventName");
        if(eventName!=null&&eventName.isEmpty()){
            sql+=(" AND sxmc = ? ");
            sqlTotal+=(" AND sxmc = ? ");
            sqlArrs.add(eventName);
            sqlTotalArrs.add(eventName);
        }

        String detailType = (String)params.get("detailType");
        if(detailType!=null&&detailType.isEmpty()){
            sql+=(" AND ywxl = ? ");
            sqlTotal+=(" AND ywxl = ? ");
            sqlArrs.add(detailType);
            sqlTotalArrs.add(detailType);
        }

        String businessType = (String)params.get("businessType");
        if(businessType!=null&&businessType.isEmpty()){
            sql+=(" AND ywlx = ? ");
            sqlTotal+=(" AND ywlx = ? ");
            sqlArrs.add(businessType);
            sqlTotalArrs.add(businessType);
        }

        String org = (String)params.get("org");
        if(org!=null&&org.isEmpty()){
            sql+=(" AND zz = ? ");
            sqlTotal+=(" AND zz = ? ");
            sqlArrs.add(org);
            sqlTotalArrs.add(org);
        }

        String guarantee = (String)params.get("guarantee");
        if(guarantee!=null&&guarantee.isEmpty()){
            sql+=(" AND zdbz = ? ");
            sqlTotal+=(" AND zdbz = ? ");
            sqlArrs.add(guarantee);
            sqlTotalArrs.add(guarantee);
        }

        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sqlTotal,sqlTotalArrs);
        while (recordSet.next()) {
            req.put("total",recordSet.getString("total"));
        }




        int pageIndex = Util.getIntValue(params.get("pageIndex").toString(), 0);
        int pageSize = Util.getIntValue(params.get("pageSize").toString(), 0);
        if(pageIndex!=0&&pageSize!=0){
            sql+="LIMIT "+pageSize+" OFFSET "+((pageIndex-1)*pageSize);
        }else {
            sql+=("LIMIT 10 OFFSET 0 ");
        }


        RecordSet rs = new RecordSet();
        rs.executeQuery(sql,sqlArrs);


        ArrayList<Map> list = new ArrayList<>();
        //7.处理结果，并获取数据
        while (rs.next()){
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> mains = new HashMap<>();
            String id = rs.getString("id");
            mains.put("cjnf",Util.null2String(rs.getString("cjnf")));
            mains.put("sxbm",Util.null2String(rs.getString("sxbm")));
            mains.put("sxmc",Util.null2String(rs.getString("sxmc")));
            mains.put("ywlx",Util.null2String(rs.getString("ywlx")));
            mains.put("ywxl",Util.null2String(rs.getString("ywxl")));
            mains.put("zz",Util.null2String(rs.getString("zz")));
            mains.put("zdbz",Util.null2String(rs.getString("zdbz")));
            mains.put("hdnf",Util.null2String(rs.getString("hdnf")));
            mains.put("glfs",Util.null2String(rs.getString("glfs")));
            mains.put("lybzbmc",Util.null2String(rs.getString("lybzbmc")));
            map.put("main",mains);
            //6.执行SQL语句命令
            //查询从表并封装
            RecordSet recordSet1 = new RecordSet();
            logger.info("预算事项底表查询...id:"+id);
            //判断参数
            String sqlDt1 = "select * from uf_yssxdb_dt1 where mainid = ? " ;
            recordSet1.executeQuery(sqlDt1,id);
            ArrayList<Map> yssxdbDt1s = new ArrayList<>();
            while(recordSet1.next()){
                HashMap<String, Object> dt1Map = new HashMap<>();
                dt1Map.put("dwjzdgz", Util.null2String(recordSet1.getString("dwjzdgz")));
                dt1Map.put("ysnd", Util.null2String(recordSet1.getString("ysnd")));
                dt1Map.put("yskm", Util.null2String(recordSet1.getString("yskm")));
                dt1Map.put("ndqcys", Util.null2String(recordSet1.getString("ndqcys")));
                dt1Map.put("nddzysnz", Util.null2String(recordSet1.getString("nddzysnz")));
                dt1Map.put("nddzyslx", Util.null2String(recordSet1.getString("nddzyslx")));
                dt1Map.put("ndys", Util.null2String(recordSet1.getString("ndys")));
                dt1Map.put("ndyfpxmys", Util.null2String(recordSet1.getString("ndyfpxmys")));
                dt1Map.put("ndsykfpys", Util.null2String(recordSet1.getString("ndsykfpys")));
                dt1Map.put("zxs", Util.null2String(recordSet1.getString("zxs")));
                dt1Map.put("zys", Util.null2String(recordSet1.getString("zys")));
                dt1Map.put("fsze", Util.null2String(recordSet1.getString("fsze")));
                dt1Map.put("ysye", Util.null2String(recordSet1.getString("ysye")));
                logger.info("预算事项底表查询...id:"+id+", 明细表2 : "+dt1Map);
                yssxdbDt1s.add(dt1Map);
            }
            logger.info("预算事项底表查询...id:"+id+", 明细表1 : "+yssxdbDt1s);
            //查询从表并封装
            map.put("ndmx",yssxdbDt1s);
            recordSet1.executeQuery("select * from uf_yssxdb_dt2 where mainid = ? ",id);
            ArrayList<Map> yssxdbDt2s = new ArrayList<>();
            while(recordSet1.next()){
                HashMap<String, Object> dt2Map = new HashMap<>();
                dt2Map.put("yskm", Util.null2String(recordSet1.getString("yskm")));
                dt2Map.put("qzqqcze", Util.null2String(recordSet1.getString("qzqqcze")));
                dt2Map.put("qzqdzed", Util.null2String(recordSet1.getString("qzqdzed")));
                dt2Map.put("qzqsxze", Util.null2String(recordSet1.getString("qzqsxze")));
                dt2Map.put("qzqylxje", Util.null2String(recordSet1.getString("qzqylxje")));
                dt2Map.put("qzqsyklx", Util.null2String(recordSet1.getString("qzqsyklx")));
                dt2Map.put("zxs", Util.null2String(recordSet1.getString("zxs")));
                dt2Map.put("zys", Util.null2String(recordSet1.getString("zys")));
                dt2Map.put("qzqljyzxje", Util.null2String(recordSet1.getString("qzqljyzxje")));
                dt2Map.put("qzqsykzxje", Util.null2String(recordSet1.getString("qzqsykzxje")));
                yssxdbDt2s.add(dt2Map);
            }
            logger.info("预算事项底表查询...id:"+id+", 明细表2 : "+yssxdbDt2s);
            map.put("qzqmx",yssxdbDt2s);
            list.add(map);
        }

        req.put("data",list);
        return req;
    }
}
